package org.pine.start.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.pine.start.filter.DruildSqlSlf4jLogFilter;
import org.pine.start.interceptor.MybatisPlusPermissionInterceptor;
import org.pine.start.interceptor.MybatisPlusSensitiveInterceptor;
import org.pine.start.interceptor.MysqlPaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.List;

/**
 * MybatisPlus配置类
 *
 * @author pine
 * @version v1.0
 * @since 2025-04-12 13:20
 */
@Configuration
@Slf4j
public class MybatisPlusConfiguration {

    @Resource
    private MybatisPlusCustomProperties mybatisPlusCustomProperties;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 分页inner拦截器
        interceptor.addInnerInterceptor(new MysqlPaginationInnerInterceptor());
        // 加解密拦截器
        MybatisPlusSensitiveInterceptor.setSensitiveTextKey(mybatisPlusCustomProperties.getSensitiveTextKey());
        MybatisPlusSensitiveInterceptor.setSensitiveTextPrefix(mybatisPlusCustomProperties.getSensitiveTextPrefix());
        interceptor.addInnerInterceptor(new MybatisPlusSensitiveInterceptor());
        // 权限拦截器
        interceptor.addInnerInterceptor(new MybatisPlusPermissionInterceptor());
        return interceptor;
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource masterDataSource(@Autowired DataSourceProperties dataSourceProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dataSourceProperties.getUrl());
        dataSource.setUsername(dataSourceProperties.getUsername());
        dataSource.setPassword(dataSourceProperties.getPassword());
        dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());

        // 打印sql
        if (mybatisPlusCustomProperties.getPrintSql()) {
            List<Filter> proxyFilters = dataSource.getProxyFilters();
            proxyFilters.add(new DruildSqlSlf4jLogFilter());
        }

        return dataSource;
    }

    @Bean
    public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
        return properties -> {
            GlobalConfig globalConfig = properties.getGlobalConfig();
            globalConfig.setBanner(false);
            // 批量插入
            globalConfig.setSqlInjector(new BatchColumnSqlInjector());
        };
    }

    /**
     * 批量插入
     */
    private static class BatchColumnSqlInjector extends DefaultSqlInjector {
        @Override
        public List<AbstractMethod> getMethodList(org.apache.ibatis.session.Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
            List<AbstractMethod> methodList = super.getMethodList(configuration, mapperClass, tableInfo);
            methodList.add(new InsertBatchSomeColumn());
            return methodList;
        }
    }
}
